<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ ctype: record.ctype })">
          {{ record.csource }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8MachineProList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { m8MachineProDelete, m8MachineProListData } from '/@/api/m8/machine/pro/m8MachinePro';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('m8.m8MachinePro');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('机床参数设置管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('数据来源'),
        field: 'csource',
        component: 'Input',
      },
      {
        label: t('来源属性'),
        field: 'cfield',
        component: 'Input',
      },
      {
        label: t('最小值'),
        field: 'minVal',
        component: 'Input',
      },
      {
        label: t('最大值'),
        field: 'maxVal',
        component: 'Input',
      },
      {
        label: t('对应参数'),
        field: 'refCode',
        component: 'Select',
        componentProps: {
          dictType: 'm8_machine_proType',
          allowClear: true,
        },
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('参数'),
      dataIndex: 'ctype',
      key: 'a.ctype',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'm8_machine_proType',
    },
    {
      title: t('数据来源'),
      dataIndex: 'csource',
      key: 'a.csource',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'm8_machine_proSource',
    },
    {
      title: t('来源属性'),
      dataIndex: 'cfield',
      key: 'a.cfield',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('最小值'),
      dataIndex: 'minVal',
      key: 'a.min_val',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('最大值'),
      dataIndex: 'maxVal',
      key: 'a.max_val',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('对应参数'),
      dataIndex: 'refCode',
      key: 'a.ref_code',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'm8_machine_proType',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑机床参数设置'),
        onClick: handleForm.bind(this, { ctype: record.ctype }),
        auth: '',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: m8MachineProListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/m8/machine/pro/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  function handleSuccess() {
    reload();
  }
</script>
